# -*- coding: utf-8 -*-
import os
import urllib2
from pydub import AudioSegment


def get_audio(word, directory = False):
    """A hehe function

    hehehe

    Args:
        word: A word you want get audio.
        directory: Save audio to this directory.
    Returns:
        hehe
    Raises:
        hehe
    """
    if not directory:
        directory = os.path.abspath('.')
    if not os.path.exists(directory):
        print "directory not exist."
        return
    try:
        a_url = "http://fanyi.baidu.com/gettts?lan=en&text="+word+"&source=web"
        data = urllib2.urlopen(a_url).read()
        with open(directory+"/bd-"+word+".mp3", "wb") as f:
            f.write(data)
            f.close()
        audio = AudioSegment.from_mp3(directory+"/bd-"+word+".mp3")
        audio.export(directory+"/bd-"+word+".mp3", format='mp3',bitrate="16k")
    except:
        print word + " can not find from baidu"
